package com.zz.study.sharding.component;

import cn.hutool.core.date.DateUtil;
import com.zz.study.sharding.entity.TableInfo;
import com.zz.study.sharding.mappers.SlaveSystemMapper;
import com.zz.study.sharding.util.DateSimple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * created by xjx on  2024-04-08 13:22
 */
@Component
public class ShardHelper {

    @Autowired
    private SlaveSystemMapper slaveSystemMapper;


    public List<String> getAllPhysicalTableNameList(String tableNamePrefix){
        List<String> returnList = new ArrayList<>();
        List<TableInfo>  tableInfoList =  getAllPhysicalTable(tableNamePrefix);
        if(!CollectionUtils.isEmpty(tableInfoList)){
            for (TableInfo tableInfo:tableInfoList){
                returnList.add(tableInfo.getTableName());
            }

        }
        return returnList;
    }

    public static String getCurrentTableName(String tableNamePrefix){
        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);
// 获取当前月
        int month = calendar.get(Calendar.MONTH) + 1;
        return tableNamePrefix + year + Integer.valueOf(month);
    }

    /**
     * 按时间递增
     * @param tableNamePrefix
     * @return
     */
    public List<TableInfo> getAllPhysicalTable(String tableNamePrefix){
        List<TableInfo>  tableInfoList =    slaveSystemMapper.queryTableInfoByName(tableNamePrefix);
        tableInfoList.sort(new Comparator<TableInfo>() {
            @Override
            public int compare(TableInfo o1, TableInfo o2) {
                String tableName1 = o1.getTableName();
                String tableName2 = o2.getTableName();
                int prefixSufixIndex = tableNamePrefix.length();
                String year1 = tableName1.substring(prefixSufixIndex,prefixSufixIndex+4);
                String month1 = tableName1.substring(prefixSufixIndex+4,tableName1.length());
                String year2 = tableName2.substring(prefixSufixIndex,prefixSufixIndex+4);
                String month2 = tableName2.substring(prefixSufixIndex+4,tableName2.length());
                Integer number1 = Integer.valueOf(year1) * 100 +  Integer.valueOf(month1);
                Integer number2 = Integer.valueOf(year2) * 100 +  Integer.valueOf(month2);

                return number1-number2;
            }
        });
        return tableInfoList;
    }

    public static String getShardTableName(String tableNamePrefix ,Date date){
        String dateTimeStr = DateUtil.formatDateTime(date);
        dateTimeStr=dateTimeStr.substring(0,7).replace("-","");
        String dateTimeStrYear = dateTimeStr.substring(0,4);
        String dateTimeMongth = dateTimeStr.substring(4,6);
        dateTimeMongth=Integer.valueOf(Integer.parseInt(dateTimeMongth)).toString();
        String tableNameSuffix = dateTimeStrYear+dateTimeMongth;
        return tableNamePrefix + tableNameSuffix;
    }

    public static String getShardTableName(String tableNamePrefix ,String dateTimeStr){
        dateTimeStr=dateTimeStr.substring(0,7).replace("-","");
        String dateTimeStrYear = dateTimeStr.substring(0,4);
        String dateTimeMongth = dateTimeStr.substring(4,6);
        dateTimeMongth=Integer.valueOf(Integer.parseInt(dateTimeMongth)).toString();
        String tableNameSuffix = dateTimeStrYear+dateTimeMongth;
        return tableNamePrefix + tableNameSuffix;
    }

    /**
     * 按时间递增
     * @param tableNamePrefix
     * @return
     */
    public List<TableInfo> getAllPhysicalTableDesc(String tableNamePrefix){
        List<TableInfo>  tableInfoList =    slaveSystemMapper.queryTableInfoByName(tableNamePrefix);
        tableInfoList.sort(new Comparator<TableInfo>() {
            @Override
            public int compare(TableInfo o1, TableInfo o2) {
                String tableName1 = o1.getTableName();
                String tableName2 = o2.getTableName();
                int prefixSufixIndex = tableNamePrefix.length();
                String year1 = tableName1.substring(prefixSufixIndex,prefixSufixIndex+4);
                String month1 = tableName1.substring(prefixSufixIndex+4,tableName1.length());
                String year2 = tableName2.substring(prefixSufixIndex,prefixSufixIndex+4);
                String month2 = tableName2.substring(prefixSufixIndex+4,tableName2.length());
                Integer number1 = Integer.valueOf(year1) * 100 +  Integer.valueOf(month1);
                Integer number2 = Integer.valueOf(year2) * 100 +  Integer.valueOf(month2);

                return number2-number1;
            }
        });
        return tableInfoList;
    }



    public static String formatDate(Date dt) {
        if (dt == null) return null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        return fmt.format(dt);
    }

    /**
     * 表格式是    xxxxx_20241   xxxx_202312这种样式
     * @param tablePrefix
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getTableListByTimeRange(String tablePrefix, Date startTime, Date endTime){
        String startTimeStr = formatDate(startTime);
        String endTimeStr = formatDate(endTime);

        String startDateStr=startTimeStr.substring(0,7).replace("-","");
        String startDateStrYear = startDateStr.substring(0,4);
        String startDateStrMongth = startDateStr.substring(4,6);
        startDateStrMongth=Integer.valueOf(Integer.parseInt(startDateStrMongth)).toString();

        String endDateStr=endTimeStr.substring(0,7).replace("-","");
        String endDateStrYear = endDateStr.substring(0,4);
        String endDateStrMongth = endDateStr.substring(4,6);
        endDateStrMongth=Integer.valueOf(Integer.parseInt(endDateStrMongth)).toString();


        DateSimple dateStart = new DateSimple(startDateStrYear,startDateStrMongth);
        DateSimple dateEnd = new DateSimple(endDateStrYear,endDateStrMongth);

        List<String> list = DateSimple.dateRangeMonth(dateStart,dateEnd);
        return list.stream().map(item->tablePrefix+item).collect(Collectors.toList());

    }
}
